import ctypes
import os

# Load the shared library
lib_path = os.path.join(os.path.dirname(__file__), "../lib/libDLL.so")  # or .dll on Windows
print(lib_path)
lib = ctypes.CDLL(lib_path)

# Define argument and return types for functions
lib.createScreen.argtypes = [ctypes.c_int, ctypes.c_int]
lib.createScreen.restype = ctypes.c_void_p

lib.destroyScreen.argtypes = [ctypes.c_void_p]
lib.destroyScreen.restype = None

lib.createCamera.argtypes = []
lib.createCamera.restype = ctypes.c_void_p

lib.destroyCamera.argtypes = [ctypes.c_void_p]
lib.destroyCamera.restype = None

lib.createWorld.argtypes = [ctypes.c_void_p]
lib.createWorld.restype = ctypes.c_void_p

lib.destroyWorld.argtypes = [ctypes.c_void_p]
lib.destroyWorld.restype = None

lib.createCube.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int]
lib.createCube.restype = ctypes.c_void_p

lib.destroyCube.argtypes = [ctypes.c_void_p]
lib.destroyCube.restype = None

lib.createRound.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_float]
lib.createRound.restype = ctypes.c_void_p

lib.destroyRound.argtypes = [ctypes.c_void_p]
lib.destroyRound.restype = None

lib.addCubeToWorld.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
lib.addCubeToWorld.restype = None

lib.addRoundToWorld.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
lib.addRoundToWorld.restype = None

lib.startRendering.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
lib.startRendering.restype = None

# Python wrapper classes
class Screen:
    def __init__(self, width, height):
        self.handle = lib.createScreen(width, height)
        
    def __del__(self):
        lib.destroyScreen(self.handle)

class Camera:
    def __init__(self):
        self.handle = lib.createCamera()
        
    def __del__(self):
        lib.destroyCamera(self.handle)

class World:
    def __init__(self, camera):
        self.handle = lib.createWorld(camera.handle)
        
    def add_cube(self, cube):
        lib.addCubeToWorld(self.handle, cube.handle)
        
    def add_round(self, round_obj):
        lib.addRoundToWorld(self.handle, round_obj.handle)
        
    def __del__(self):
        lib.destroyWorld(self.handle)

class Cube:
    def __init__(self, x, y, z, size):
        self.handle = lib.createCube(x, y, z, size)
        
    def __del__(self):
        lib.destroyCube(self.handle)

class Round:
    def __init__(self, x, y, z, radius):
        self.handle = lib.createRound(x, y, z, radius)
        
    def __del__(self):
        lib.destroyRound(self.handle)

def start_rendering(screen, world):
    lib.startRendering(screen.handle, world.handle)

# Example usage
if __name__ == "__main__":
    screen = Screen(800, 800)
    camera = Camera()
    world = World(camera)
    
    # Add a cube and a round
    cube = Cube(0, 0, -500, 100)
    #round_obj = Round(20, 0, 0, 5.0)
    
    world.add_cube(cube)
    #world.add_round(round_obj)
    
    # Start rendering
    start_rendering(screen, world)